package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanMessageMaster;
import com.tsingtree.modular.entity.NanSellerTag;
import com.tsingtree.modular.entity.NanUser;
import com.tsingtree.modular.model.params.NanMessageMasterParam;
import com.tsingtree.modular.service.NanMessageMasterService;
import com.tsingtree.modular.service.NanSellerTagService;
import com.tsingtree.modular.service.NanUserService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 站内信总览控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanMessageMaster")
public class NanMessageMasterController extends BaseController {

    private String PREFIX = "/nanMessageMaster";

    @Autowired
    private NanMessageMasterService nanMessageMasterService;

    @Autowired
    private NanSellerTagService nanSellerTagService;

    @Autowired
    private NanUserService nanUserService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanMessageMaster.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanMessageMaster_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanMessageMaster_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanMessageMasterParam nanMessageMasterParam) {
        this.nanMessageMasterService.add(nanMessageMasterParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanMessageMasterParam nanMessageMasterParam) {
        this.nanMessageMasterService.update(nanMessageMasterParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
//    @RequestMapping("/delete")
//    @Permission
//    @ResponseBody
//    public ResponseData delete(NanMessageMasterParam nanMessageMasterParam) {
//        LoginUser admin = LoginContextHolder.getContext().getUser();
//        NanMessageMaster nanMessageMaster = nanMessageMasterService.getById(nanMessageMasterParam.getId());
//        nanMessageMasterService.del(nanMessageMaster);
//        return ResponseData.success();
//    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanMessageMasterParam nanMessageMasterParam) {
        NanMessageMaster detail = this.nanMessageMasterService.getById(nanMessageMasterParam.getId());
        Map<Integer, String> tagMap = nanSellerTagService.list().stream().collect(Collectors.toMap(NanSellerTag::getId, NanSellerTag::getName));
        String tag = detail.getTagId();
        if(StringUtils.hasText(tag)){
            String[] tags = tag.split(",");
            StringBuilder names = new StringBuilder();
            for(String tagStr : tags){
                int key = Integer.parseInt(tagStr.substring(1, tagStr.length() - 1));
                String name = tagMap.get(key);
                names.append(" | ").append(name);
            }
            detail.setTagStr(names.substring(3));
        }
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanMessageMasterParam nanMessageMasterParam) {
        return this.nanMessageMasterService.findPageBySpec(nanMessageMasterParam);
    }

}


